﻿$(function () {
    $("img.lazy").lazyload({
        effect: "fadeIn",
        failure_limit: 3,
        data_attribute: 'src',
        placeholder: '/Content/images/lazyplaceholder.png'
    });

    $(document.body).on('click', 'button[data-editor]', function () {
        var me = $(this);
        document.refId = me.data('refid');
        document.imageSrc = me.data('imgsrc');
        switch (me.data('editor')) {
            case 'create':
            case 'edit':
                getForm(document.refId, document.currentCulture, document.imageSrc);
                break;
            case 'remove':
                $('#confirmDialog').modal('show');
                break;
        }
    });

    $(document.body).on('click', '.langarea button', function () {
        var culture = $(this).data('culture');
        getForm($('#editorModal form').data('refid'), culture, $('#editorModal form').data('imgsrc'));
    });

    $('#editorModal').on('show.bs.modal', function () {
        $('#editorModalLoading').show();
        $('#confirmDialog .modal-body').hide();
        $('#confirmDialog .modal-footer').hide();
    });

    $(document.body).on('submit', '#editorModal form', function (e) {
        var files = $('#editorModal form input[name="Image"]').get(0).files;

        if (files.length > 0 && $('#editorModal form input[name="Src"]').val() === '') {
            e.preventDefault();

            var data = new FormData();
            for (var x = 0; x < files.length; x++) {
                data.append("file" + x, files[x]);
            }

            $.ajax(document.uploadUrl, {
                type: "POST",
                data: data,
                contentType: false,
                processData: false,
                success: function (result) {
                    switch (result.status) {
                        case 200:
                            var obj = $.parseJSON(result.object);
                            $('#editorModal form input[name="Src"]').val(obj[0]);
                            $('#editorModal form').submit();
                            break;
                        default:
                            Messenger().post({
                                message: result.message,
                                type: 'error',
                                showCloseButton: true
                            });
                            break;
                    }
                }
            });

            return false;
        }

        return true;
    });

    $('#confirmDialog button[data-yes]').click(function () {
        removeModel(document.refId, document.currentCulture, document.imageSrc);
    });
});

function getForm(refid, culture, imageSrc) {
    $('#editorModal').modal('show');
    $.get('EditPageImage', { refId: refid, selectedCulture: culture, imageSrc: imageSrc }, function (result) {
        $('#editorModalLoading').hide();
        $('#formctnr').html(result);
        $('#editorModal .langarea button:not([data-culture=' + culture + '])').removeClass('btn-primary').addClass('btn-default');
        $('#editorModal .langarea button[data-culture=' + culture + ']').addClass('btn-primary');
        $('#confirmDialog .modal-body').show();
        $('#confirmDialog .modal-footer').show();
    });
}

function removeModel(refid, culture, imageSrc) {
    $.ajax('RemovePageImage', {
        data: { refid: refid, selectedCulture: culture, imageSrc: imageSrc },
        type: "POST",
        beforeSend: function () {
            $('#confirmDialogLoading').show();
            $('#confirmDialog .modal-body').hide();
            $('#confirmDialog .modal-footer').hide();
        },
        success: function (result) {
            debugger;
            switch (result.status) {
                case 200:
                    $('.media[data-src="' + imageSrc + '"]').remove();
                    break;
                default:
                    Messenger().post({
                        message: result.message,
                        type: 'error',
                        showCloseButton: true
                    });
                    break;
            }
        }
    }).always(function () {
        $('#confirmDialog').modal('hide');
        $('#confirmDialogLoading').hide();
    });;
}